//
//  GW_Tool+time.m
//  IMHuanXin
//
//  Created by 小獾科技 on 2017/7/27.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "GW_Tool+time.h"

@implementation GW_Tool (time)


+ (NSString *)timeStringFromInterval:(NSTimeInterval)timeInterval {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];//1404145800000
    if (!date) {                                                             //1416908304
        return @"";
    }
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    return [fmt stringFromDate:date];// 返回UTC时间
}

#pragma mark - May 03, 2016
+ (NSString *)timeStringFromUTCDateString:(NSString *)dateString {
    
    if ([self isEmptyString:dateString]) {
        return @"";
    }
    // dateString = 2016-5-23 07:02:45
    //    NSDate *date = [self getLocalDateFromUTCDateString:dateString];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    fmt.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSDate *utcDate = [fmt dateFromString:dateString];// 传入的是UTC时间，传出的也是UTC时间
    
    // 通过 [NSCalendar currentCalendar] 转过来的时间 默认是本地时间
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:utcDate];
    
    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];
    
    NSString *monthStr = @"";
    switch (month) {
        case 1:
        {
            monthStr = @"Jan";
        }
            break;
        case 2:
        {
            monthStr = @"Feb";
        }
            break;
        case 3:
        {
            monthStr = @"Mar";
        }
            break;
        case 4:
        {
            monthStr = @"Apr";
        }
            break;
        case 5:
        {
            monthStr = @"May";
        }
            break;
        case 6:
        {
            monthStr = @"Jun";
        }
            break;
        case 7:
        {
            monthStr = @"Jul";
        }
            break;
        case 8:
        {
            monthStr = @"Aug";
        }
            break;
        case 9:
        {
            monthStr = @"Sep";
        }
            break;
        case 10:
        {
            monthStr = @"Oct";
        }
            break;
        case 11:
        {
            monthStr = @"Nov";
        }
            break;
        case 12:
        {
            monthStr = @"Dec";
        }
            break;
        default:
            break;
    }
    NSString *retStr = [NSString stringWithFormat:@"%@ %02zd, %zd",monthStr,day,year];
    
    return retStr;
}

#pragma mark - N天前
+ (NSString *)spaceTimeStringFromUTCDateString:(NSString *)dateString {
    
    if ([self isEmptyString:dateString]) {
        return @"";
    }
    
    // dateString = 2016-5-23 07:02:45
    //    NSDate *date = [self getLocalDateFromUTCDateString:dateString];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    fmt.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSDate *utcDate = [fmt dateFromString:dateString];// 传入的是UTC时间，传出的也是UTC时间
    
    // 通过 [NSCalendar currentCalendar] 转过来的时间 默认是本地时间
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:utcDate];
    
    //    NSInteger year = [components year];
    //    NSInteger month = [components month];
    //    NSInteger day = [components day];
    //    NSInteger hour = [components hour];
    //    NSInteger minute = [components minute];
    //    NSInteger second = [components second];
    
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDateComponents *currentComponents = [currentCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:[NSDate date]];
    
    
    NSString *title = @"";
    
    NSDate *nowDate = [NSDate date];// UTC日期
    
    NSTimeInterval timeInt = [nowDate timeIntervalSinceDate:utcDate];
    if (timeInt < 0) {
        
        // 日期出错
        title = [self dateToString:utcDate withFormat:@"yyyy-MM-dd HH:mm"];
        
    } else {
        
        if ([components year] < [currentComponents year]) {
            
            title = [self dateToString:utcDate withFormat:@"yyyy-MM-dd HH:mm"];// 跨年
            
        } else if ([components month] < [currentComponents month]) {
            
            title = [self dateToString:utcDate withFormat:@"MM-dd HH:mm"];// 跨月
            
        } else {
            
            //天
            //            NSInteger day = timeInt / (24*60*60);
            
            // 除了天，剩余秒数
            NSInteger leftSecond = (NSInteger)timeInt % (24*60*60);
            
            //小时
            NSInteger hour = leftSecond / (60*60);
            
            leftSecond = leftSecond % (60*60);
            
            //分钟
            NSInteger minute = leftSecond / 60;
            
            if ([currentComponents day] - [components day] > 1) {
                
                title = [self dateToString:utcDate withFormat:@"MM-dd HH:mm"];
                
            } else if ([currentComponents day] - [components day] == 1) {
                
                title = [NSString stringWithFormat:@"Yesterday %@",[self dateToString:utcDate withFormat:@"HH:mm"]];
                
            } else if (hour > 0 || minute > 0) {
                
                title = [NSString stringWithFormat:@"%@",[self dateToString:utcDate withFormat:@"HH:mm"]];
                
            } else {
                
                title = @"Just Now";
            }
            
            //            if ([components month] < [currentComponents month]) {
            //
            //                title = [NSString stringWithFormat:@"%zd month ago", [currentComponents month]-[components month]];
            //
            //            } else if ([components day] < [currentComponents day]) {
            //
            //                title = [NSString stringWithFormat:@"%zd day ago", [currentComponents day]-[components day]];
            //
            //            } else if ([components hour] < [currentComponents hour]) {
            //
            //                title = [NSString stringWithFormat:@"%zd hour ago", [currentComponents hour]-[components hour]];
            //
            //            } else {
            //
            //                title = @"Just Now";
            //            }
        }
        
    }
    
    
    return title;
}

#pragma mark - 时区相关
+ (NSDate *)getLocalDateFromUTCDateString:(NSString *)dateString {
    
    return [self getLocalDateFromUTCDateString:dateString withFormat:@"yyyy-MM-dd HH:mm:ss"];
}

+ (NSDate *)getLocalDateFromUTCDateString:(NSString *)dateString withFormat:(NSString *)format{
    
    // dateString = 2016-6-8 08:02:45
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = format;
    fmt.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSDate *utcDate = [fmt dateFromString:dateString];
    
    NSDate* destinationDateNow = [self getLocalDateFromUTCDate:utcDate];
    
    return destinationDateNow;
}

+ (NSDate *)getLocalDateFromUTCDate:(NSDate *)utcDate {
    
    //设置源日期时区
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    //设置转换后的目标日期时区
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    //得到源日期与世界标准时间的偏移量
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:utcDate];
    //目标日期与本地时区的偏移量
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:utcDate];
    //得到时间偏移量的差值
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    //转为现在时间
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:utcDate];
    
    return destinationDateNow;
}

+ (NSDate *)getLocalDate {
    
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:[NSDate date]];
    NSDate *localDate = [[NSDate date] dateByAddingTimeInterval: interval];
    
    return localDate;
}

+ (NSString *)getLocalDateString {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"]; //HH:mm:ss
    formatter.timeZone = [NSTimeZone localTimeZone];
    NSString *localString = [formatter stringFromDate: [NSDate date]];
    
    return localString;
}

#pragma mark - other

//NSString 2 NSDate
+ (NSDate *)stringToDate:(NSString *)strdate {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *retdate = [dateFormatter dateFromString:strdate];
    
    return retdate;
}

//NSDate 2 NSString
+ (NSString *)dateToString:(NSDate *)date withFormat:(NSString *)format {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];// @"yyyy-MM-dd HH:mm:ss"
    NSString *strDate = [dateFormatter stringFromDate:date];
    
    return strDate;// 传入UTC时间，传出local时间  UTC是世界标准时间
}

//将本地日期字符串转为UTC日期字符串
//本地日期格式:2013-08-03 12:53:51
//可自行指定输入输出格式
+ (NSString *)getUTCFormateLocalDate:(NSString *)localDate {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    
    return dateString;
}

// 将UTC日期字符串转为本地时间字符串
// 输入的UTC日期格式2013-08-03T04:53:51+0000
+ (NSString *)getLocalDateFormateUTCDate:(NSString *)utcDate {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    
    NSDate *dateFormatted = [dateFormatter dateFromString:utcDate];
    //输出格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    
    return dateString;
}


@end
